<?php

namespace common\models;

use Yii;
use common\models\SmsWeb;
/**
 * This is the model class for table "sms_web".
 *
 * @property string $id
 * @property string $user_id
 * @property integer $type
 * @property string $relation_id
 * @property string $content
 * @property integer $is_read
 * @property string $remark
 * @property integer $is_delete
 * @property string $create_time
 * @property string $update_time
 */
class SmsWeb extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sms_web';
    }

   /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'content'], 'required'],
            [['user_id', 'type', 'relation_id', 'is_read', 'is_delete'], 'integer'],
            [['content'], 'string'],
            [['create_time', 'update_time'], 'safe'],
            [['remark'], 'string', 'max' => 255],
        ];
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'user_username' => '用户账号',
            'type' => '站内信类型：1、默认类型；',
            'relation_id' => '关联业务id',
            'content' => '内容',
            'is_read' => '是否已读',
            'remark' => '备注',
            'is_delete' => '是否删除',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

     public static function itemAlias ($type, $code = NULL)
    {
        $_items = [
            'is_read' => [
                //''  =>'全部',
                '1' => "未读",              
                '2' => "已读",
            ],
            'is_delete' => [
               // ''  =>'是否使用',
                '1' => "未删",              
                '2' => "已删",
                                
            ],
           
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

     public static function model($className=__CLASS__){
        return parent::model($className);
    }


public static function sendWeb($arr=array()){

            $id = $arr['id'];
            switch ($arr['type']) {
                case 1:
                    $web_content = '恭喜注册成功';
                    break;
                /*case 2:
                    $web_content = str_replace(array('{userName}'), array($arr['userName']), $web_template);
                    break;*/
         
              /*  default:
                    $web_content = $web_template;*/
            }
            //发送站内信
            $smsWebModel = new SmsWeb;
            $smsWebModel->attributes = array(
                'user_id' => $id,
                'type' => $arr['type'],
                //'relation_id' => $arr['relation_id'],
                'content' => $web_content,
                'is_read' => 1,
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => '0000-00-00 00:00:00',
            );
            if($smsWebModel->save(false)){
                return 1;
            }else{
                return 2;
            }
        }
       






    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                $this->create_time = date("Y-m-d H:i:s", time());
                $this->update_time = date("Y-m-d H:i:s", time());
            }else{
                $this->update_time = date("Y-m-d H:i:s", time());
            }
            return true;
        } else {
            return false;
        }
    } 
}
